package com.qs.education.bean.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;

import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.dao.IEducationTeacherDao;
import com.qs.education.bean.entity.EducationStudent;
import com.qs.education.bean.entity.EducationTeacher;

/**
 * @class EducationSMSCodeDaoImpl
 * @author Leon
 * @description
 * @date Nov 2, 2018
 */
@Repository

public class EducationTeacherDaoImpl extends BaseDaoImpl<EducationTeacher> implements IEducationTeacherDao {

	@Override
	public EducationTeacher findEducationTeacherByMobile(Map<String, Object> params) {
		String jql = "Select ea from EducationTeacher ea where ea.mobile =:mobile";
		List<EducationTeacher> list = query(jql, EducationTeacher.class, params);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<EducationTeacher> findTeacherByCondition(EducationTeacher teacher) {
		
		String jql = "from EducationTeacher es where 1 = 1";
		Map<String, Object> params = new HashMap<>();
		if (teacher.getSchool()!=null&&teacher.getSchool().getName()!="") {
			jql+=" and es.school.name like :school";
			params.put("school", "%"+teacher.getSchool().getName()+"%");
		}
		if (teacher.getMobile()!=null&&teacher.getMobile()!="") {
			jql+=" and es.mobile like :mobile";
			params.put("mobile", "%"+teacher.getMobile()+"%");
		}
		if (teacher.getName()!=null&&teacher.getName()!="") {
			jql+=" and es.name like :name";
			params.put("name", "%"+teacher.getName()+"%");
		}
		if (teacher.getSubject()!=null&&teacher.getSubject().getId()!=null) {
			jql+=" and es.subject = :subject";
			params.put("subject", teacher.getSubject());
		}
		List<EducationTeacher> result = query(jql, EducationTeacher.class, params);
		
		return result;
	}
	
	@Override
	public List<EducationTeacher> findEducationTeacherByMobileOrName(String content) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("content", content);
		String jql = "Select ea from EducationTeacher ea where ea.mobile =:content or ea.name =:content";
		List<EducationTeacher> list = query(jql, EducationTeacher.class, params);
		return list;
	}

	@Override
	public int getTeacherCount(EducationTeacher teacher) {
		
		String jql = "Select COUNT(*)  from EducationTeacher es where 1 = 1";
		Map<String, Object> params = new HashMap<>();
		if (teacher.getSchool()!=null && teacher.getSchool().getName()!=null && teacher.getSchool().getName()!="") {
			jql+=" and es.school.name like :school";
			params.put("school", "%"+teacher.getSchool().getName()+"%");
		}
		if (teacher.getMobile()!=null && teacher.getMobile()!="") {
			jql+=" and es.mobile like :mobile";
			params.put("mobile", "%"+teacher.getMobile()+"%");
		}
		if (teacher.getName()!=null && teacher.getName()!="") {
			jql+=" and es.name like :name";
			params.put("name", "%"+teacher.getName()+"%");
		}
		if (teacher.getSubject()!=null && teacher.getSubject().getId()!=null) {
			jql+=" and es.subject = :subject";
			params.put("subject", teacher.getSubject());
		}
		List<?> result = query(jql,params);
		if(null!=result && !result.isEmpty()) {
			return Integer.parseInt(result.get(0).toString());
		}
		
		return 0;
	}

	@Override
	public List<EducationTeacher> getTeacher(EducationTeacher teacher, Integer currentPage, Integer pageSize) {

		String jql = "Select es  from EducationTeacher es where 1 = 1";
		Map<String, Object> params = new HashMap<>();
		if (teacher.getSchool()!=null && teacher.getSchool().getName()!=null && teacher.getSchool().getName()!="") {
			jql+=" and es.school.name like :school";
			params.put("school", "%"+teacher.getSchool().getName()+"%");
		}
		if (teacher.getMobile()!=null && teacher.getMobile()!="") {
			jql+=" and es.mobile like :mobile";
			params.put("mobile", "%"+teacher.getMobile()+"%");
		}
		if (teacher.getName()!=null && teacher.getName()!="") {
			jql+=" and es.name like :name";
			params.put("name", "%"+teacher.getName()+"%");
		}
		if (teacher.getSubject()!=null && teacher.getSubject().getId()!=null) {
			jql+=" and es.subject = :subject";
			params.put("subject", teacher.getSubject());
		}
		int firstResult = (currentPage - 1) * pageSize;
		List<EducationTeacher> list = query(jql,EducationTeacher.class,firstResult,pageSize,params);
		
		return list;
	}
}
